package com.kust.smarthome.web.utils;

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
import java.util.Random;

@Component
public class EmailUtils {

//    @Bean
    public String sendEmail(String email) throws GeneralSecurityException, MessagingException {
        String code = this.produceCode();
        Properties p = new Properties();

        p.setProperty("mail.host", "smtp.qq.com");
        p.setProperty("mail.transport.protocol", "smtp");
        p.setProperty("mail.smtp.auth", "true");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        p.put("mail.smtp.ssl.enable", "true");
        p.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getDefaultInstance(p, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("1748707603@qq.com", "aklikbqdloyahddf");
            }
        });

        session.setDebug(true);

        Transport transport = session.getTransport();

        transport.connect("smtp.qq.com", "1748707603@qq.com", "aklikbqdloyahddf");

        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress("1748707603@qq.com"));

        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));

        mimeMessage.setSubject("向日葵邮箱找回密码验证");

        mimeMessage.setContent("您收到的验证码为:" + code, "text/html;charset=UTF-8");

        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

        transport.close();

        return code;
    }

//    @Bean
    public void sendEmail(String email, String title, String info) throws GeneralSecurityException, MessagingException {

        Properties p = new Properties();

        p.setProperty("mail.host", "smtp.qq.com");
        p.setProperty("mail.transport.protocol", "smtp");
        p.setProperty("mail.smtp.auth", "true");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        p.put("mail.smtp.ssl.enable", "true");
        p.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getDefaultInstance(p, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("1748707603@qq.com", "aklikbqdloyahddf");
            }
        });

        session.setDebug(true);

        Transport transport = session.getTransport();

        transport.connect("smtp.qq.com", "1748707603@qq.com", "aklikbqdloyahddf");

        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress("1748707603@qq.com"));

        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));

        mimeMessage.setSubject(title);

        mimeMessage.setContent("请点击以下链接进行注册:" +info, "text/html;charset=UTF-8");

        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

        transport.close();

    }

    private String produceCode() {
        Random rand = new Random();
        char[] cs = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        String s = "";
        for (int i = 0; i < 4; i++) {
            s += cs[rand.nextInt(62)];
        }
        return s;
    }


}
